﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class HealthBar
    {
        private Scene scene;
        private Ship playerShip;

        private Texture2D healthImgGreen;
        private Texture2D healthImgYellow;
        private Texture2D healthImgRed;

        private SpriteBatch spriteBatch;

        private int scrWidth;
        private int scrHeight;

        private int offset = 10;
        
        public HealthBar(Scene scene, Ship playerShip)
        {
            this.scene = scene;
            this.playerShip = playerShip;

            this.spriteBatch = ShooterGame.Instance.SpriteBatch;

            this.healthImgGreen = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\life-green");
            this.healthImgYellow = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\life-yellow");
            this.healthImgRed = ShooterGame.Instance.Content.Load<Texture2D>("Textures\\hud\\life-red");

            this.scrWidth = ShooterGame.Instance.GraphicsDevice.PresentationParameters.BackBufferWidth;
            this.scrHeight = ShooterGame.Instance.GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        public void Update(GameTime gameTime)
        {
        }

        public void Draw(GameTime gameTime)
        {
            Texture2D healthImg;

            Rectangle destRect = new Rectangle();
            destRect.Width = 16;
            destRect.Height = 32;

            float healthRatio = playerShip.Health / playerShip.MaxHealth;
            int health;
            if (healthRatio > 0.0f && healthRatio < 0.1f)
                health = 1;
            else
                health = (int)(10.0f * healthRatio);

            if (health > 7)
                healthImg = healthImgGreen;
            else if (health > 3)
                healthImg = healthImgYellow;
            else
                healthImg = healthImgRed;

            destRect.Y = scrHeight - destRect.Height - offset;

            spriteBatch.Begin(SpriteBlendMode.Additive);

            for (int i = 0; i < health; i++)
            {
                destRect.X = scrWidth - 20 - (i + 1) * (destRect.Width );
                
                spriteBatch.Draw(healthImg, destRect, Color.White);
            }
            spriteBatch.End();
        }

    }
}
